package com.qitancai.huazistudentmanagement.controller;

import com.qitancai.huazistudentmanagement.common.api.Result;
import com.qitancai.huazistudentmanagement.entity.Student;
import com.qitancai.huazistudentmanagement.service.StudentSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName StudentController
 * @Description 学生信息控制层
 * @Author 
 * @Date 2021年06月22日 2:13
 **/
@RequestMapping("/api/students")
@RestController
public class StudentController {

    @Autowired
    private StudentSevice studentSevice;

    @GetMapping("/{studentId}")
    public Result<Student> getStudentById(@PathVariable("studentId") String id) {
        Student result = studentSevice.getStudentById(id);
        if (result == null) {
            return Result.failed("查询失败");
        }
        return Result.success(result);
    }

    @PostMapping("/create")
    public Result<Student> createStudent(@RequestBody Student student) {
        Student result = studentSevice.createStudent(student);
        if (result == null) {
            return Result.failed("新建学生信息失败");
        }
        return Result.success(result);
    }

    @PutMapping("/{studentId}")
    public Result<Student> updateStudent(@PathVariable String studentId, @RequestBody Student student) {
        student.setId(studentId);
        Student result = studentSevice.updateStudent(student);
        if (result == null) {
            return Result.failed("更新学生信息失败");
        }
        return Result.success(result);
    }

    @DeleteMapping("/{studentId}")
    public Result<Void> deleteStudent(@PathVariable String studentId) {
        studentSevice.deleteStudent(studentId);
        return Result.success(null);
    }

    @PostMapping("/query")
    public Result<Page> queryStudent(@RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by("id"));
        Page page = studentSevice.queryStudent(pageable);
        if (page == null) {
            return Result.failed("查询失败");
        }
        return Result.success(page);
    }


    @GetMapping("/queryByName")
    public Result<Page> queryStudentByName(@RequestParam String name,
                                     @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by("id"));
        Page page = studentSevice.queryStudentByName(name, pageable);
        if (page == null) {
            return Result.failed("查询失败");
        }
        return Result.success(page);
    }

}
